package com.yunhai.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunhai.common.dto.GlobalException;
import com.yunhai.common.dto.PaginationVo;
import com.yunhai.common.utils.ObjectUtil;
import com.yunhai.common.utils.ReflectionCopy;
import com.yunhai.model.entity.sys.SysNoticeMessageEntity;
import com.yunhai.model.entity.sys.VersionInfo;
import com.yunhai.model.vo.sys.SysNoticeMessageQuery;
import com.yunhai.model.vo.sys.SysNoticeMessageVo;
import com.yunhai.system.mapper.SysNoticeMessageMapper;
import com.yunhai.system.service.SysNoticeMessageService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SysNoticeMessageServiceImpl implements SysNoticeMessageService {

    @Resource
    private SysNoticeMessageMapper sysNoticeMessageMapper;

    @Override
    public IPage getNoticeMessagePagination(PaginationVo paginationVo) throws Exception {
        IPage page = new Page( paginationVo.getPageIndex(), paginationVo.getPageSize() );
        LambdaQueryWrapper<SysNoticeMessageEntity> lqs = new LambdaQueryWrapper<>();
        lqs.orderByDesc(SysNoticeMessageEntity::getCreateTime);
        sysNoticeMessageMapper.selectPage(page, lqs);

        return page;
    }

    @Override
    @Transactional
    public void addNoticeMessage(SysNoticeMessageVo sysNoticeMessageVo) throws Exception {

        SysNoticeMessageEntity sysNoticeMessageEntity = new SysNoticeMessageEntity();
        ReflectionCopy.copyProperties( sysNoticeMessageEntity, sysNoticeMessageVo );
        sysNoticeMessageMapper.insert(sysNoticeMessageEntity);
        return;
    }


    @Override
    public void updateNoticeMessage(SysNoticeMessageVo sysNoticeMessageVo) throws Exception {
        SysNoticeMessageEntity sysNoticeMessageEntity = new SysNoticeMessageEntity();
        ReflectionCopy.copyProperties( sysNoticeMessageEntity, sysNoticeMessageVo );
        sysNoticeMessageMapper.updateById(sysNoticeMessageEntity);
        return;
    }

    @Override
    @Transactional
    public void batchRemoveNoticeMessage(List<Long> ids) throws Exception {
        if( !ObjectUtil.isNotEmptyList(ids) ){
            throw GlobalException.gen(10011, "要删除的id不存在");
        }
        sysNoticeMessageMapper.deleteBatchIds(ids);
    }

    @Override
    public SysNoticeMessageEntity getNoticeMessageDetail(Long id) throws Exception {

        SysNoticeMessageEntity sysNoticeMessageEntity = sysNoticeMessageMapper.selectById(id);
        if( sysNoticeMessageEntity == null ){
            throw GlobalException.gen(11011, "通知不存在");
        }
        return sysNoticeMessageEntity;
    }
}
